package com.smedi.ismedi.constructionproject.core.application.service.impl;

import com.smedi.ismedi.constructionproject.core.application.command.ConstructionProjectUpdateCommand;
import com.smedi.ismedi.constructionproject.core.application.command.ProjectBasicCommand;
import com.smedi.ismedi.constructionproject.core.application.command.ProjectBasicUpdateCommand;
import com.smedi.ismedi.constructionproject.core.application.command.ProjectListQueryCommand;
import com.smedi.ismedi.constructionproject.core.application.convert.AppConverter;
import com.smedi.ismedi.constructionproject.core.application.query.ProjectBasicQueryService;
import com.smedi.ismedi.constructionproject.core.application.service.ConstructionProjectApplicationService;
import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.ProjectBasic;
import com.smedi.ismedi.constructionproject.core.domain.repository.ProjectBasicRepository;
import com.smedi.ismedi.constructionproject.core.domain.service.BidSectionDomainService;
import com.smedi.ismedi.constructionproject.core.domain.service.ProjectAssignmentDomainService;
import com.smedi.ismedi.constructionproject.core.domain.service.ProjectBasicDomainService;
import com.smedi.ismedi.constructionproject.core.infrastructure.enumerate.EpcTypeEnum;
import com.smedi.ismedi.constructionproject.core.infrastructure.enumerate.ProjectCategoryEnum;
import com.smedi.ismedi.constructionproject.core.infrastructure.enumerate.ProjectStatusEnum;
import com.smedi.ismedi.constructionproject.core.port.adapter.service.OrganizationRemoteApiService;
import com.smedi.ismedi.organization.api.dto.response.HrOrganizationResponse;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Collections;

/**
 * @Author: xulinjun
 * @Date: 2021-6-21 10:27
 */
@Slf4j
@Service
@AllArgsConstructor
public class ConstructionProjectApplicationServiceImpl implements ConstructionProjectApplicationService {
    private final DozerUtil dozerUtil;

    private final ProjectBasicDomainService projectBasicDomainService;

    private final ProjectBasicQueryService projectBasicQueryService;

    private final OrganizationRemoteApiService organizationRemoteApiService;

    private final ProjectAssignmentDomainService projectAssignmentDomainService;

    private final BidSectionDomainService bidSectionDomainService;

    private final ProjectBasicRepository projectBasicRepository;

    private final AppConverter appConverter;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ProjectBasic saveProjectBasicInfo(ProjectBasicCommand projectBasicCommand) {
        ProjectBasic projectBasic = dozerUtil.convertor(projectBasicCommand, ProjectBasic.class);
        projectBasic = projectBasicDomainService.saveInfo(projectBasic);

        // 获取间隔天数
        projectBasic.setContractDurationDays(projectBasicDomainService.getDurationDays(projectBasic.getContractDurationBegin(), projectBasic.getContractDurationEnd()))
                .setActualDurationDays(projectBasicDomainService.getDurationDays(projectBasic.getActualDurationBegin(), projectBasic.getActualDurationEnd()))
                .setProjectCategoryMeaning(ProjectCategoryEnum.getName(projectBasic.getProjectCategory()))
                .setProjectStatusMeaning(ProjectStatusEnum.getName(projectBasic.getProjectStatus()))
                .setEpcTypeMeaning(EpcTypeEnum.getName(projectBasic.getEpcType()));

        // 获取组织信息
        HrOrganizationResponse hrOrganizationResponse = organizationRemoteApiService.getHrOrganizationView(projectBasic.getHrOrganizationId());
        if (hrOrganizationResponse != null) {
            projectBasic.setHrOrganizationFullName(hrOrganizationResponse.getFullName())
                    .setHrOrganizationShortName(hrOrganizationResponse.getShortName());
        }

        return projectBasic;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateProjectBasicInfo(ProjectBasicUpdateCommand projectBasicUpdateCommand) {
        ProjectBasic projectBasic = dozerUtil.convertor(projectBasicQueryService.getProjectList(ProjectListQueryCommand.builder().ids(Collections.singletonList(projectBasicUpdateCommand.getProjectId())).build()).get(0),ProjectBasic.class);
        ProjectBasic projectBasicEdited = appConverter.updateProjectBasicByProjectBasicUpdateCommand(projectBasic,projectBasicUpdateCommand);
        return projectBasicDomainService.updateInfo(projectBasicEdited);
    }

    @Override
    public boolean validateHrOrgHasProject(Long hrOrganizationId) {
        return projectBasicDomainService.validateHrOrgHasProject(hrOrganizationId);
    }

    @Override
    public boolean validateProject(Long projectId) {
        return projectBasicDomainService.validateProject(projectId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteProjectAllInfos(Long projectId) {
        // 删除项目信息
        projectBasicDomainService.deleteProjectBasic(projectId);
        // 删除项目分配信息
        projectAssignmentDomainService.deleteAssignmentByProjectId(projectId);
        // 删除标段信息
        bidSectionDomainService.deleteByProjectId(projectId);
    }

    @Override
    public ProjectBasic postSaveVividProgress(Long projectId, int vividProgress, LocalDate vividProgressUpdatedDate) {
        ProjectBasic projectBasic = projectBasicRepository.selectByProjectId(projectId);
        if (null == projectBasic.getVividProgressUpdatedDate() || (null != projectBasic.getVividProgressUpdatedDate() && !vividProgressUpdatedDate.isBefore(projectBasic.getVividProgressUpdatedDate()))) {
            projectBasic.setVividProgress(vividProgress);
            projectBasic.setVividProgressUpdatedDate(vividProgressUpdatedDate);
            projectBasicRepository.doUpdate(projectBasic);
            return projectBasic;
        }
        return projectBasic;
    }

    @Override
    public ProjectBasic postSaveRegionPicture(Long projectId, String regionPicture) {
        ProjectBasic projectBasic = projectBasicRepository.selectByProjectId(projectId);
        projectBasic.setRegionPicture(regionPicture);
        projectBasicRepository.doUpdate(projectBasic);
        return projectBasic;
    }

    @Override
    public ProjectBasic postSaveOrganizationPicture(Long projectId, String organizationPicture) {
        ProjectBasic projectBasic = projectBasicRepository.selectByProjectId(projectId);
        projectBasic.setOrganizationPicture(organizationPicture);
        projectBasicRepository.doUpdate(projectBasic);
        return projectBasic;
    }

    @Override
    public ProjectBasic postSaveEntrustBook(Long projectId, String entrustBook) {
        ProjectBasic projectBasic = projectBasicRepository.selectByProjectId(projectId);
        projectBasic.setEntrustBook(entrustBook);
        projectBasicRepository.doUpdate(projectBasic);
        return projectBasic;
    }

    @Override
    public ProjectBasic postSaveMonitorType(Long projectId, String monitorType,String monitorTypeNote) {
        ProjectBasic projectBasic = projectBasicRepository.selectByProjectId(projectId);
        projectBasic.setMonitorType(monitorType);
        projectBasic.setMonitorTypeNote(monitorTypeNote);
        projectBasicRepository.doUpdate(projectBasic);
        return projectBasic;

    }

    @Override
    public long updateProjectByName(ConstructionProjectUpdateCommand command) {
        return projectBasicDomainService.updateProjectByName(command.getProjectName(),command.getDingDeptKey());
    }
}
